package com.ithinks.flow.common.json;


import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.*;
import net.sf.json.util.JSONUtils;
import net.sf.json.util.PropertyFilter;

import java.util.*;

/**
 * @Description
 * @ClassName JsonUtils
 * @Author MR.LI
 * @date 2020.12.03 18:00
 */
public class JsonUtils {

    /**
     * 将数组转换为json
     * @param object
     * @return
     */
    public static String array2json(Object object){
        JSONArray jsonArray = JSONArray.fromObject(object);
        return jsonArray.toString();
    }

    /**
     * 将json转换为数组
     * @param json
     * @param valueClz
     * @return
     */
    public static Object json2Array(String json,Class valueClz){
        JSONArray jsonArray = JSONArray.fromObject(json);
        return JSONArray.toArray(jsonArray,valueClz);
    }


    /**
     * 将集合转换为json
     * @param object
     * @return
     */
    public static String collection2json(Object object){
        JSONArray jsonArray = JSONArray.fromObject(object);
        return jsonArray.toString();
    }

    /**
     * 将map转换为json
     * @param object
     * @return
     */
    public static String map2json(Object object){
        JSONArray jsonArray = JSONArray.fromObject(object);
        return jsonArray.toString();
    }

    /**
     * json转map
     * @return
     */
    public static Map<?,?> json2Map(Object object){

        JSONObject jsonObject = null;
        if(object instanceof JSONObject){
            jsonObject = (JSONObject) object;
        }else{
            jsonObject = JSONObject.fromObject(jsonObject);
        }
        Map<Object,Object> map = new HashMap<Object,Object>(jsonObject.size());
        Iterator<?> it = jsonObject.keys();
        while (it.hasNext()){
            Object key = it.next();
            Object value = jsonObject.get(key);
            if(value instanceof JSONObject){
                map.put(key,json2Map(value));
            }else if(value instanceof JSONObject) {
                map.put(key,jsonArrToList(value));//
            }else{
                map.put(key,value);
            }
        }
        return map;
    }

    /**
     * list串或jsonArray对象转成json
     * @param obj
     * @return
     */
    public static List<?> jsonArrToList(Object obj){
        JSONArray jsonArray = null;
        if(obj instanceof JSONArray){
            jsonArray = (JSONArray) obj;
        }else{
            jsonArray = JSONArray.fromObject(obj);
        }
        List list = new ArrayList(jsonArray.size());
        Iterator<?> it = jsonArray.iterator();

        while(it.hasNext()){
            Object next = it.next();
            if(next instanceof JSONObject){
                list.add(json2Map(next));
            }else{
                list.add(next);
            }
        }
        return list;

    }
    /**
     * json串转java对象
     * @param jsonStr json串
     * @param rootClass 要转成的主对象
     * @param subClassMap 主对象中包含的list类型的属性Map 格式：subClassMap.put("iordersegments", IorderSegment.class); key是子对象在主对象中的属性名， value是子对象类型
     * @return
     */
    public static <T>T jsonToBean(String jsonStr,Class<T> rootClass,Map<String, Class> subClassMap){
        JsonConfig filterNullConfig = new JsonConfig();
        //过滤掉参数值为null的参数，防止后边的时间转换出错
        filterNullConfig.setJsonPropertyFilter(new PropertyFilter() {
            @Override
            public boolean apply(Object clazz, String name, Object value) {
                boolean isFilter = false;
                if(value==null||"".equals(value)){
                    isFilter = true;
                }
                return isFilter;
            }
        });

        JSONObject jsonObject = JSONObject.fromObject(jsonStr,filterNullConfig);

        String[] dateFormats = new String[] {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd","yyyy-MM-dd HH:mm"};//不过好像只有   yyyy-MM-dd HH:mm:ss 格式有效
        JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));

        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setRootClass(rootClass);
        if(subClassMap!=null&&subClassMap.size()>0){
            jsonConfig.setClassMap(subClassMap);
        }

        return (T) JSONSerializer.toJava(jsonObject, jsonConfig);
    }
}
